home *** CD-ROM | disk | FTP | other *** search
/ Network Support Library / RoseWare - Network Support Library.iso / apidev / mgabra.arc / MGMMACS.ASM < prev    next >
Assembly Source File  |  1988-04-17  |  14KB  |  527 lines

  1. ;------------------------------------------------
  2. ; WHMACS - Assembly Language Macros
  3. ;------------------------------------------------
  4.  
  5. ;----------------------------------------------------------
  6. ; SAVREGS - Save 8086 Registers To Tcb (task control block)
  7. ;----------------------------------------------------------
  8.  
  9. SAVREGS MACRO   TASK
  10.         mov     [TASK], 0    ;0 means process put to sleep
  11.         mov     [TASK+2], ax
  12.         mov     [TASK+4], bx
  13.         mov     [TASK+6], cx
  14.         mov     [TASK+8], dx
  15.         mov     [TASK+10], bp
  16.         mov     [TASK+12], di
  17.         mov     [TASK+14], si
  18.         mov     [TASK+16], es
  19.         mov     [TASK+18], ds
  20.         pop     [TASK+24]       ;ip
  21.         pop     [TASK+26]       ;cs
  22.         pop     [TASK+28]       ;flags
  23.         mov     [TASK+20], ss
  24.         mov     [TASK+22], sp
  25.         ENDM
  26.  
  27. ;----------------------------------------------------
  28. ; SAVCUR - Save Cursor Position To Tcb. We use direct
  29. ; hardware because some program bypass the bios and
  30. ; give an innaccurate cursor position.
  31. ;----------------------------------------------------
  32.  
  33. SAVCUR  MACRO   TASK
  34. ;    mov    dx, add6845    ;find 6845 controller
  35. ;    mov    al, 14        ;register number
  36. ;    out    dx, al        ;do it
  37. ;    inc    dx        ;set for next register
  38. ;    in    al, dx
  39. ;    mov    ah, al
  40. ;    dec    dx              ;reset dx
  41. ;    mov    al, 15        ;port number
  42. ;    out    dx, al
  43. ;    inc    dx
  44. ;    in    al, dx
  45. ;    and    ax, 07ffh
  46. ;    mov    bl, 80
  47. ;    div    bl
  48. ;    xchg    ah, al
  49. ;       mov     [TASK+30], ax    ;save cursor location
  50.     mov    bx, 0
  51.     push    bx
  52.     pop    es            ;set segment
  53.     mov    bx, 0462h        ;page number in low ram
  54.     mov    bh, byte ptr es:[bx]    ;get it
  55.     mov    ah, 3
  56.     int    10h
  57.     mov    [TASK+30], dx        ;cursor position
  58.     mov    [TASK+52], cx        ;cursor size
  59.         ENDM
  60.  
  61. ;------------------------------------------------
  62. ; RESCUR - Restore Cursor Address From Tcb
  63. ;------------------------------------------------
  64.  
  65. RESCUR  MACRO   TASK
  66.     LOCAL    LB1, LB2
  67.     mov    ax, cs:scrswp    ;get screen swap mode
  68.     and    ax, 1        ;is cursor swapping enabled?
  69.     jnz     LB2        ;yes then do it
  70.     mov    ax, cs:scrswp    ;check again
  71.     and    ax, 8        ;cursor swap on key switch?
  72.     jz    LB1        ;if not then exit
  73.     cmp    cs:keyswap, 1    ;was it done on a keyswap?
  74.     jne    LB1        ;no then exit
  75. LB2:
  76.         mov     dx, [TASK+30]    ;cursor row and column to dx
  77.     mov    bh, byte ptr [TASK+51]    ;page number
  78.         mov     ah, 2        ;set cursor address function number
  79.         int     10h        ;call bios
  80.     mov    cx, [TASK+52]    ;cursror size
  81.     mov    ah, 1
  82.     int    10h
  83. LB1:
  84.         ENDM
  85.  
  86. ;------------------------------------------------
  87. ; SAVDTA - SAVE DTA ADDRESS TO TCB
  88. ;------------------------------------------------
  89.  
  90. SAVDTA  MACRO   TASK
  91.         mov     ah, 2fh        ;get dta address function number
  92.         int     21h        ;call dos
  93.         mov     [TASK+32], es    ;save dta address segment
  94.         mov     [TASK+34], bx    ;save dta offset
  95.         ENDM
  96.  
  97. ;------------------------------------------------
  98. ; SAV24H - Save Critical Error Address To Tcb
  99. ;------------------------------------------------
  100.  
  101. SAV24h  MACRO   TASK
  102.         SAVINT  24h, [TASK+36], [TASK+38]
  103.         ENDM
  104.  
  105. ;------------------------------------------------
  106. ; SAV23H - Save Control Break Handler
  107. ;------------------------------------------------
  108.  
  109. SAV23h    MACRO    TASK
  110.     SAVINT    23h, [TASK+46], [TASK+48]
  111.     ENDM
  112.  
  113. ;------------------------------------------------
  114. ; SAVPRC - Save Entire Process
  115. ;------------------------------------------------
  116.  
  117. SAVPRC  MACRO   TASK
  118.         SAVREGS TASK
  119.      SAVDTA    TASK
  120.      SAV24H    TASK
  121.     SAV23H    TASK
  122.     SAVCUR    TASK
  123.      SAVSCR  TASK
  124.         ENDM
  125.  
  126. ;------------------------------------------------
  127. ; SAVPAG - Save current video page to TCB
  128. ;------------------------------------------------
  129.  
  130. SAVPAG    MACRO    TASK
  131.     mov    bx, 0
  132.     push    bx
  133.     pop    es            ;set segment
  134.     mov    bx, 0462h        ;page number in low ram
  135.     mov    al, byte ptr es:[bx]    ;get it
  136.     mov    ah, 0
  137.     mov    [TASK+50], ax           ;save it into TCB
  138.     mov    cs:videop, ax        ;move it to our global variable
  139.      call    VIDSEG             ;calculate effective video base
  140.     ENDM
  141.  
  142. ;------------------------------------------------
  143. ; SETPAG - set video page
  144. ;------------------------------------------------
  145.  
  146. SETPAG    MACRO    PG
  147.     mov    ax, PG               ;get value page
  148.     mov    ah, 5            ;set video page function number
  149.     int    10h            ;set it
  150.     ENDM
  151.  
  152. ;------------------------------------------------
  153. ; RESPAG - Restore video page
  154. ;------------------------------------------------
  155.  
  156. RESPAG    MACRO    TASK
  157.     mov    ax,  [TASK+50]        ;get value of this processes page
  158.     mov    cs:videop, ax           ;save it into our variable
  159.     mov    ah, 5            ;set video page function number
  160.     int    10h            ;set it
  161.     call     VIDSEG            ;calculate video base
  162.     ENDM
  163.  
  164. ;------------------------------------------------
  165. ; SAVMOD - Save Video Mode To Tcb
  166. ;------------------------------------------------
  167.  
  168. SAVMOD    MACRO    TASK
  169.     mov    bx, 0
  170.     push    bx
  171.     pop    es            ;set segment 0
  172.     mov    bx, 0449h        ;set offset
  173.     mov    al, es:[bx]        ;get video mode
  174.     mov    ah, 0            ;clear ah
  175.     mov    [TASK+40], ax        ;save it
  176.     mov    cs:videom, ax        ;place in global mode variable
  177.     ENDM
  178.  
  179. ;------------------------------------------------
  180. ; RESMOD - Restore Video Mode
  181. ;------------------------------------------------
  182.  
  183. RESMOD    MACRO    TASK
  184.     LOCAL    resmod1
  185.     mov    ax, [tsrprg+40]        ;compare our video modes
  186.     cmp    ax, [dosprg+40]        ;if not different don't switch
  187.     je    resmod1            ;because mode swith is unaesthetic
  188.     mov    ax, [TASK+40]        ;set mode number from TCB
  189.     mov    cs:videom, ax        ;place in global mode variable
  190.     mov    ah, 0            ;set video mode function number
  191.     int    10h            ;call bios
  192.  
  193. resmod1:
  194.  
  195.     ENDM
  196.  
  197. ;------------------------------------------------
  198. ; LODTCK Load tasks tick allocation
  199. ;------------------------------------------------
  200.  
  201. LODTCK    MACRO    TASK
  202.     push    [TASK+44]        ;get it's ticks
  203.     pop    cs:period        ;load them into master timer
  204.     ENDM
  205.  
  206. ;------------------------------------------------
  207. ; SAVSCR - SAVE SCREEN CONTENTS TO BUFFER
  208. ;------------------------------------------------
  209.  
  210. SAVSCR    MACRO    TASK
  211.     LOCAL    LB1, LB2, LB3, LB4
  212.     mov    ax, cs:scrswp    ;get screen swap parameter
  213.     and    ax, 2        ;is save screen bit set?
  214.     jnz    LB4        ;yes then swap 'em
  215.                 ;else ...
  216.     mov    ax, cs:scrswp    ;check again for swap by key
  217.     and    ax, 16        ;save if swap by key?
  218.     jz    LB1        ;no, then exit outta here
  219.                 ;else ...
  220.     cmp    cs:keyswap, 1    ;was swap done by key?
  221.     jne     LB1        ;no, then exit.
  222. LB4:
  223.     SAVMOD    TASK        ;save video mode
  224.      SAVPAG    TASK        ;save video page
  225.     push    ds        ;duck ds!
  226.     push    cs        ;get cs into es
  227.     pop    es
  228.     mov    di, [TASK+42]    ;set destination offset
  229.     mov    ds, cs:videos    ;set source segment
  230.     mov    si, 0        ;set source offset
  231.     mov    cx, cs:videosz    ;initialize count
  232.     cld
  233. rep    movsb            ;buffer transfer
  234.  
  235.     pop    ds        ;restore ds
  236. LB1:
  237.     ENDM
  238.  
  239. ;--------------------------------------------------
  240. ; RESSCR - Restore screen from buffer to video ram
  241. ;--------------------------------------------------
  242.  
  243. RESSCR    MACRO    TASK
  244.     LOCAL    LB1, LB2, LB3, LB4, SNOW1, SNOW2, NOSNOW
  245.     mov    ax, cs:scrswp    ;get screen swap mode
  246.     and    ax, 4        ;is screen restoration enabled?
  247.     jnz    LB4        ;yes? do it
  248.     mov    ax, cs:scrswp    ;check again for swap by key
  249.     and    ax, 32        ;save if swap by key?
  250.     jz    LB1        ;no, then exit outta here
  251.     cmp    cs:keyswap, 1    ;was swap done by key?
  252.     jne     LB1        ;no, then exit.
  253. LB4:
  254.      RESMOD    TASK        ;restore video mode
  255.      RESPAG    TASK        ;restore video page
  256.     push    ds        ;duck ds!
  257.     push    cs        ;get cs into ds
  258.     pop    ds
  259.     mov    si, [TASK+42]    ;set source offset, assume cs seg
  260.     mov    es, cs:videos    ;set destination segment
  261.     mov    di, 0        ;set destination offset
  262.     mov    cx, cs:videosz    ;initialize count
  263.     mov    ax, cs:scrswp
  264.     and    ax, 256        ;check for snow/no snow option
  265.     jnz    NOSNOW
  266.     cld
  267.     mov    dx, 03dah    ;address of video controller
  268. SNOW1:
  269.     in    al, dx
  270.     and    al, 1
  271.     jnz    SNOW1
  272. SNOW2:
  273.     in    al, dx
  274.     and    al, 1
  275.     jz    SNOW2
  276.         movsb            ;buffer transfer
  277.     loop    SNOW1
  278.     pop    ds        ;restore ds
  279.     jmp    LB1
  280.  
  281. NOSNOW:
  282.     cld
  283. rep    movsb
  284.     pop    ds
  285. LB1:
  286.     ENDM
  287.  
  288. ;------------------------------------------------
  289. ; RES23H - Restore Control Break Handler
  290. ;------------------------------------------------
  291.  
  292. RES23h    MACRO    TASK
  293.     SETINT    23h, [TASK+46], [TASK+48]
  294.     ENDM
  295.  
  296. ;------------------------------------------------
  297. ; RESREGS - Restore 8086 From Tcb
  298. ;------------------------------------------------
  299.  
  300. RESREGS MACRO   TASK
  301.         mov     [TASK], 1    ;1 means process awakened
  302.         mov     ax, [TASK+2]
  303.         mov     bx, [TASK+4]
  304.         mov     cx, [TASK+6]
  305.         mov     dx, [TASK+8]
  306.         mov     bp, [TASK+10]
  307.         mov     di, [TASK+12]
  308.         mov     si, [TASK+14]
  309.         mov     es, [TASK+16]
  310.         mov     ds, [TASK+18]
  311.     cli
  312.         mov     ss, [TASK+20]
  313.         mov     sp, [TASK+22]
  314.     sti
  315.         push    [TASK+28]       ;flags
  316.         push    [TASK+26]       ;cs
  317.         push    [TASK+24]       ;ip
  318.         ENDM
  319.  
  320. ;------------------------------------------------
  321. ; RESDTA - Restore Dta Address From Tcb
  322. ;------------------------------------------------
  323.  
  324. RESDTA  MACRO   TASK
  325.         mov     ds, [TASK+32]    ;restore dta segment
  326.         mov     dx, [TASK+34]    ;restore dta offset
  327.         mov     ah, 1ah        ;set dta function number
  328.         int     21h        ;call dos
  329.         ENDM
  330.  
  331. ;------------------------------------------------
  332. ; RES24H Restore Critical Error Handler From Tcb
  333. ;------------------------------------------------
  334.  
  335. RES24h  MACRO   TASK
  336.         SETINT  24h, [TASK+36], [TASK+38]
  337.         ENDM
  338.  
  339. ;------------------------------------------------
  340. ; RESPRC - Restore Entire Process
  341. ;------------------------------------------------
  342.  
  343. RESPRC  MACRO   TASK
  344.      RESSCR    TASK
  345.     RESCUR    TASK
  346.     RES23H    TASK
  347.       RES24H    TASK
  348.       RESDTA    TASK
  349.      LODTCK    TASK
  350.         RESREGS TASK
  351.         ENDM
  352.  
  353. ;------------------------------------------------
  354. ; TSR - Terminate And Stay Resident
  355. ;------------------------------------------------
  356.  
  357. TSR     MACRO   PARA
  358.         mov     dx, PARA    ;put paragraphs to save in dx
  359.     mov    ah, 31h        ;tsr function number
  360.         int     21h        ;call dos
  361.         ENDM
  362.  
  363. ;--------------------------------------------------
  364. ; EOI - Restore Interrupts To 8259 - reset hardware
  365. ;--------------------------------------------------
  366.  
  367. EOI     MACRO
  368.         cli            ;don't interrupt this!
  369.         push    ax        ;get ax out of the way
  370.         mov     al, 20h        ;enable interrupts value 20h
  371.         out     20h, al        ;send to port 20h
  372.                 ;it is only a coincidence that the
  373.                 ;values are the same!
  374.         pop     ax        ;restore ax
  375.         sti            ;re-enable interrupts
  376.         ENDM
  377.  
  378. ;-------------------------------------------------------
  379. ; BEEP - Sound Bell - Utility macro, not used in program
  380. ;-------------------------------------------------------
  381.  
  382. BEEP    MACRO
  383.         mov     ah, 14
  384.         mov     al, 7
  385.         int     10h
  386.         ENDM
  387.  
  388. ;------------------------------------------------
  389. ; CHIRP
  390. ;------------------------------------------------
  391.  
  392. CHIRP    MACRO    QUAL
  393.     LOCAL    cp1
  394.     push    cx
  395.     push    ax
  396.     in    ax, 97
  397.     and    ax, 0fch
  398.     mov    cx, QUAL
  399. cp1:
  400.     out    97, ax
  401.     inc    ax
  402.     inc    ax
  403.     out    97, ax
  404.     dec    ax
  405.     dec    ax
  406.     loop    cp1
  407.     pop    ax
  408.     pop    cx
  409.     ENDM
  410.  
  411. ;-------------------------------------------------------------------------
  412. ; PUTCHAR - Put A Character To Screen - Utility macro, not used in program
  413. ;-------------------------------------------------------------------------
  414.  
  415. PUTCHR  MACRO   CHR
  416.     push    ax
  417.         mov     ah, 14
  418.         mov     al, CHR
  419.         int     10h
  420.     pop    ax
  421.         ENDM
  422.  
  423. ;------------------------------------------------
  424. ; SETINT - Set An Interrupt Vector
  425. ;------------------------------------------------
  426.  
  427. SETINT  MACRO   INTR, SEG, OFF
  428.         push    ds        ;get ds out of the way
  429.         push    SEG        ;get segment into ds
  430.         pop     ds
  431.         mov     dx, OFF        ;get offset into dx
  432.         mov     al, INTR    ;put interrupt number into al
  433.         mov     ah, 25h        ;set interrupt, function number
  434.         int     21h        ;call dos
  435.         pop     ds        ;restore ds
  436.         ENDM
  437.  
  438. ;------------------------------------------------
  439. ; SAVINT - Save An Interrupt Vector
  440. ;------------------------------------------------
  441.  
  442. SAVINT  MACRO   INTR, SEG, OFF
  443.         mov     al, INTR    ;move interrupt number into al
  444.         mov     ah, 35h        ;get interrupt function number
  445.         int     21h        ;call dos
  446.         mov     SEG, es        ;save vector segment
  447.         mov     OFF, bx        ;save vector offset
  448.         ENDM
  449.  
  450. ;------------------------------------------------
  451. ; SHWCHR - put character to video ram
  452. ;------------------------------------------------
  453.  
  454. SHWCHR  MACRO   CHAR, LOC
  455.         push    es
  456.         push    ax
  457.     push    bx
  458.     mov    ax, 0b800h
  459.     push    ax
  460.     pop    es
  461.         mov     al, CHAR
  462.     mov    ah, 23
  463.         mov     bx, LOC
  464.     mov    es:[bx], ax
  465.     pop    bx
  466.         pop     ax
  467.         pop     es
  468.     ENDM
  469.  
  470. ;------------------------------------------------
  471. ; MOVMEM - copy a block of memory
  472. ;------------------------------------------------
  473.  
  474. MOVMEM    MACRO    SSEG, SOFF, DSEG, DOFF, NUM
  475.     push    ds
  476.     push    si
  477.     push    es
  478.     push    di
  479.     push    cx
  480.  
  481.     push    DOFF
  482.     push    DSEG
  483.     push    SOFF
  484.     push    SSEG
  485.  
  486.     pop    ds          ;set source segment
  487.     pop    si          ;set source offset
  488.     pop    es          ;set destination segment
  489.     pop    di          ;set destination offset
  490.     cld            ;clear direction flag
  491.     mov    cx, NUM        ;number of bytes to move
  492. rep    movsb
  493.  
  494.     pop     cx
  495.     pop     di
  496.     pop     es
  497.     pop     si
  498.     pop     ds
  499.  
  500.     ENDM
  501.  
  502. ;-----------------------------------------------------
  503. ; ABORT - takes an error code and $ terminated message
  504. ;-----------------------------------------------------
  505.  
  506. ABORT    MACRO    CODE, MESSAGE
  507.     push    ds
  508.     mov    ax, 0
  509.     call    clear
  510.     push    cs
  511.     pop    ds
  512.     mov    dx, offset err0
  513.     mov    ah, 9
  514.     int    21h
  515.     mov    dx, offset MESSAGE
  516.     mov    ah, 9
  517.     int    21h
  518.     pop    ds
  519.     mov    ah, 4ch
  520.     mov    al, CODE
  521.     int    21h
  522.     ENDM
  523.  
  524. ;------------------------------------------------
  525. ; END OF WHMACS
  526. ;------------------------------------------------
  527.